import React, { useEffect, useState } from 'react';
import { Button } from 'antd'; // or your UI library's Button
import styles from './index.module.less'; // adjust path as needed
import { IBelong } from '@/utils/excel';
import { useSessionContext } from '../../../content';
import { set } from 'lodash';
import { ICompany, IPerson } from '@/ts/core';

interface MemberStatsProps {
  space: IBelong;
}

const MemberStatsCard: React.FC<MemberStatsProps> = ({ space }) => {
  const [totalMembers, setTotalMembers] = useState(0); // 成员数
  const [departmentNum, setDepartmentNum] = useState(0); // 部门数
  const [GroupNum, setGroupNum] = useState(0); //群组数
  const [clusterNum, setClusterNum] = useState(0); //设立集群数
  const [joinClusterNum, setJoinClusterNum] = useState(0); //加入集群数
  const [friend, setFriend] = useState(0); //好友数
  const [createdUnitNum, setCreatedUnitNum] = useState(0); //设立单位数
  const [joinUnitNum, setJoinUnitNum] = useState(0); //加入单位数
  const [createdGroupNum, setCreatedGroupNum] = useState(0); //设立群组数
  const [joinedGroupNum, setJoinedGroupNum] = useState(0); //加入群组数
  const { updateContent } = useSessionContext();
  const [isPerson, setIsPerson] = useState(false);
  useEffect(() => {
    const loadData = async () => {
      if (space.typeName == '人员') {
        setIsPerson(true);
        const friends = await (space as IPerson).loadMembers();
        setFriend(friends.length);
        let createdUnitNum = 0;
        let joinUnitNum = 0;
        let createdGroupNum = 0;
        let joinedGroupNum = 0;
        for (const unit of (space as IPerson).companys) {
          if (unit.metadata.createUser == space.id) {
            createdUnitNum = createdUnitNum + 1;
          } else {
            joinUnitNum = joinUnitNum + 1;
          }
        }
        setCreatedUnitNum(createdUnitNum);
        setJoinUnitNum(joinUnitNum);
        for (const group of (space as IPerson).cohorts) {
          if (group.metadata.createUser == space.id) {
            createdGroupNum = createdGroupNum + 1;
          } else {
            joinedGroupNum = joinedGroupNum + 1;
          }
        }
        setCreatedGroupNum(createdGroupNum);
        setJoinedGroupNum(joinedGroupNum);
      } else if (space.typeName == '单位') {
        const members = await space.loadMembers();
        const departments = await (space as ICompany).loadDepartments();
        const groups = await (space as ICompany).loadGroups();
        let groupNum = space.cohorts.length;
        let clusterNum = 0;
        let joinClusterNum = 0;
        for (const group of groups) {
          if (group.typeName == '组织群') {
            if (group.belongId == space.id) {
              clusterNum = clusterNum + 1;
            } else {
              joinClusterNum = joinClusterNum + 1;
            }
          }
        }
        setTotalMembers(members.length);
        setDepartmentNum(departments.length);
        setGroupNum(groupNum);
        setClusterNum(clusterNum);
        setJoinClusterNum(joinClusterNum);
      }
    };
    loadData();
  }, [space]);
  return (
    <div className={styles.cardContainer}>
      <div className={styles.cardHeader}>
        <h3 className={styles.title}>关系</h3>
        <Button
          type="link"
          onClick={() => {
            updateContent('relation');
          }}
          className={styles.moreButton}>
          更多 &gt;
        </Button>
      </div>

      {!isPerson ? (
        <div className={styles.statsGrid}>
          <div className={styles.statItem}>
            <div className={styles.statLabel}>成员数</div>
            <div className={styles.statValue}>{totalMembers}</div>
          </div>

          <div className={styles.statItem}>
            <div className={styles.statLabel}>部门数</div>
            <div className={styles.statValue}>{departmentNum}</div>
          </div>

          <div className={styles.statItem}>
            <div className={styles.statLabel}>群组数</div>
            <div className={styles.statValue}>{GroupNum}</div>
          </div>

          <div className={styles.statItem}>
            <div className={styles.statLabel}>设立集群数</div>
            <div className={styles.statValue}>{clusterNum}</div>
          </div>

          <div className={styles.statItem}>
            <div className={styles.statLabel}>加入集群数</div>
            <div className={styles.statValue}>{joinClusterNum}</div>
          </div>
        </div>
      ) : (
        <div className={styles.statsGrid}>
          <div className={styles.statItem}>
            <div className={styles.statLabel}>好友</div>
            <div className={styles.statValue}>{friend}</div>
          </div>

          <div className={styles.statItem}>
            <div className={styles.statLabel}>设立单位数</div>
            <div className={styles.statValue}>{createdUnitNum}</div>
          </div>

          <div className={styles.statItem}>
            <div className={styles.statLabel}>加入单位数</div>
            <div className={styles.statValue}>{joinUnitNum}</div>
          </div>

          <div className={styles.statItem}>
            <div className={styles.statLabel}>设立群组数</div>
            <div className={styles.statValue}>{createdGroupNum}</div>
          </div>
          <div className={styles.statItem}>
            <div className={styles.statLabel}>加入群组数</div>
            <div className={styles.statValue}>{joinedGroupNum}</div>
          </div>
        </div>
      )}
    </div>
  );
};

export default MemberStatsCard;
